package compiler.ast;

import java.io.PrintStream;

import compiler.tac.BasicBlock;
import compiler.tac.ExitBlock;
import compiler.tac.Instruction.Operator;
import compiler.tac.ThreeAddressCode;

public class ReturnStatement extends Statement {
	//Expression returnValue;
	
	public ReturnStatement(Expression val) {
		this.returnValue = val;
	}

	@Override
	public Expression getreturnValue()
	{
		return returnValue;
	}
	public void printDotFormat(PrintStream out, String node) {
		out.println(node+" [label=\"return\"];");
		
		if (returnValue != null) {
			String exprNode = node+"0";
			out.println(node+"->"+exprNode+";");
			returnValue.printDotFormat(out, exprNode);
		}
	}

	@Override
	public BasicBlock toThreeAddressCode(ThreeAddressCode tac, BasicBlock prev,
			ExitBlock exit) {
		if (returnValue != null) {
			String t = returnValue.toThreeAddressCode(tac, prev);
			prev.addUnaryInstruction(Operator.Mv, ThreeAddressCode.returnTemp, t);
		}
		prev.setHasReturnValue(returnValue != null);
		
		prev.addReturnInstruction();
		BasicBlock.add(prev, exit);
		
		return null;
	}
}
